package com.erp.common.response;

import com.erp.common.enums.ResponseCode;
import com.erp.common.exception.BaseBizException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
public class BaseResponse<T> implements Serializable {
    private static final long serialVersionUID = -5133484959332030789L;
    /**
     * 响应状态码
     */
    protected int code;

    /**
     * 响应信息
     */
    protected String message;

    /**
     * 响应数据
     */
    protected T data;

    /**
     * 附加信息
     */
    @JsonIgnore
    protected Map<String, Object> extra;

    public BaseResponse() {
    }

    public BaseResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public BaseResponse(int code, String message, T data, Map<String, Object> extra) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.extra = extra;
    }

    public static <T> BaseResponse<T> success() {
        BaseResponse<T> result = result(ResponseCode.OK.getCode(), ResponseCode.OK.getMsg(), null, null);
        return result;
    }

    public static <T> BaseResponse<T> success(int code, String message) {
        BaseResponse<T> result = result(code, message, null, null);
        return result;
    }

    public static <T> BaseResponse<T> success(int code, T data, String message) {
        BaseResponse<T> result = result(code, message, data, null);
        return result;
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> result = result(ResponseCode.OK.getCode(), ResponseCode.OK.getMsg(), data, null);
        return result;
    }

    public static <T> BaseResponse<T> success(T data, Map<String, Object> extra) {
        BaseResponse<T> result = result(ResponseCode.OK.getCode(), ResponseCode.OK.getMsg(), data, extra);
        return result;
    }

    public static <T> BaseResponse<T> fail(String message) {
        BaseResponse<T> result = result(ResponseCode.BUSINESS_ERROR.getCode(), message, null, null);
        return result;
    }

    public static <T> BaseResponse<T> fail(int code, String message) {
        BaseResponse<T> result = result(code, message, null, null);
        return result;
    }

    public static <T> BaseResponse<T> fail(int code, T data, String message) {
        BaseResponse<T> result = result(code, message, data, null);
        return result;
    }


    public static <T> BaseResponse<T> result(int code, String message, T data, Map<String, Object> extra) {
        BaseResponse<T> tCommonResponse = new BaseResponse<T>(code, message, data, extra);
        return tCommonResponse;
    }

    public static <T> BaseResponse<T> judge(boolean isSuccess) {
        if (isSuccess) {
            BaseResponse<T> result = result(ResponseCode.OK.getCode(), ResponseCode.OK.getMsg(), null, null);
            return result;
        }
        BaseResponse<T> result = result(ResponseCode.BUSINESS_ERROR.getCode(), ResponseCode.BUSINESS_ERROR.getMsg(), null, null);
        return result;
    }
}
